﻿using Abp.Domain.Entities;
using Batman.Jianbei.Forum.Core.Domains.Users;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Batman.Jianbei.Forum.Core.Domains.Posts
{
    /// <summary>
    /// 着色表（统称） 点赞表/收藏表 
    /// </summary>
    public class PostTint : Entity<string>
    {
        public PostTint()
        {
            CreateAt = DateTime.Now;
        }

        public PostTint(string postId, string wxUserId) : this()
        {
            PostId = postId;
            WxUserId = wxUserId;
        }

        public string WxUserId { get; set; }

        public string PostId { get; set; }

        /// <summary>
        /// 着色类型 点赞/收藏
        /// </summary>
        public PostTintType Type { get; set; }

        public DateTime CreateAt { get; set; }

        public Post Post { get; set; }

        public WeixinUser WxUser { get; set; }

        /// <summary>
        /// 创建点赞实体对象
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="wxUserId"></param>
        /// <returns></returns>
        public static PostTint NewLike(string postId, string wxUserId)
        {
            PostTint postTint = new PostTint(postId, wxUserId);

            postTint.Type = PostTintType.Like;

            return postTint;
        }

        /// <summary>
        /// 创建收藏实体对象
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="wxUserId"></param>
        /// <returns></returns>
        public static PostTint NewFavorite(string postId, string wxUserId)
        {
            PostTint postTint = new PostTint(postId, wxUserId);

            postTint.Type = PostTintType.Favorite;

            return postTint;
        }

    }


    public static class PostTintEumerableExtension
    {
        /// <summary>
        /// 包含某个用户是否点赞
        /// </summary>
        /// <param name="query">查询语句句柄</param>
        /// <param name="wxUId">微信用户ID</param>
        /// <returns></returns>
        public static bool ContainHisLike(this IEnumerable<PostTint> query, string wxUId)
        {
            return query.Any(t => t.Type == PostTintType.Like && t.WxUserId == wxUId);
        }

        /// <summary>
        /// 包含某个用户是否收藏
        /// </summary>
        /// <param name="query">查询语句句柄</param>
        /// <param name="wxUId">微信用户ID</param>
        /// <returns></returns>
        public static bool ContainHisFavorite(this IEnumerable<PostTint> query, string wxUId)
        {
            return query.Any(t => t.Type == PostTintType.Favorite && t.WxUserId == wxUId);
        }
    }
}
